unit HSV;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, Math, StdCtrls;

const
  h= 40;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Timer1: TTimer;
    Timer2: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
    procedure Draw;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  mass: array of integer;
  massP: array of TPanel;
  massS: array of TPanel;
  n, i, m, t, j,k :integer;
  fin: textfile;
  
implementation

{$R *.dfm}

procedure Heapify (t, q: integer);
var a,b: integer;
begin
  if (q=1) then
    Form1.Timer1.Enabled:=false;
  if (q=2) then
    Form1.Timer2.Enabled:=false;
  while (1=1) do
  begin
    a:=2000000;
    b:=2000000;
    if (2*t+1<n) then
      a:=mass[2*t+1];
    if (2*t+2<n) then
      b:=mass[2*t+2];
    if (a<=b) and (a<mass[t]) then
    begin
      mass[t]:=mass[t]+mass[2*t+1];
      mass[2*t+1]:=mass[t]-mass[2*t+1];
      mass[t]:=mass[t]-mass[2*t+1];
      t:=t*2+1;
    end else
    begin
      if (b<a) and (b<mass[t]) then
      begin
        mass[t]:=mass[t]+mass[2*t+2];
        mass[2*t+2]:=mass[t]-mass[2*t+2];
        mass[t]:=mass[t]-mass[2*t+2];
        t:=2*t+2;
      end else
        break;
    end;
    Form1.Draw;
  end;
  if (q=1) then
    Form1.Timer1.Enabled:=true;
  if (q=2) then
    Form1.Timer2.Enabled:=true;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Timer2.Enabled:=false;
  Timer1.Enabled:=false;
  AssignFile (fin, 'input.txt');
  Reset (fin);
  readln (fin, n);
  setlength (mass, n);
  for i:= 0 to n-1 do
    read (fin, mass[i]);
  setlength (massP, n);
  for i:= 0 to n-1 do
  begin
    massP[i]:=TPanel.Create(Form1);
    massP[i].Color:=clWhite;
    massP[i].Caption:=IntToStr(mass[i]);
    massP[i].Height:=h;
    massP[i].Width:=h;
    massP[i].Top:=10+trunc(Log2(i+1))*(10+h);
    t:=1;
    for j:=1 to trunc(log2(i+1)) do
      t:=t*2;
    massP[i].Left:=(800 div (t+1))*((i+1) mod t + 1) - h div 2;
    massP[i].Parent:=Form1;
  end;
  setlength (massS, n);
  for i:= 0 to n-1 do
  begin
    massS[i]:=TPanel.Create(Form1);
    massS[i].Color:=clWhite;
    massS[i].Height:=2*h;
    massS[i].Width:=2*h;
    massS[i].Top:=300;
    massS[i].Left:= i*2*h;
    massS[i].Parent:=Form1;
  end;
  k:=n-1;
  closefile (fin);
  Timer1.Enabled:=true;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if (k=-1) then
  begin
    Timer1.Enabled:=false;
    Timer2.Enabled:=true;
    m:=n;
    k:=1;
    exit;
  end;
  heapify (k, 1);
  k:=k-1;
end;

procedure TForm1.Timer2Timer(Sender: TObject);
begin
  if (k>m-1) then
  begin
    Timer2.Enabled:=false;
    n:=m;
    massS[k-1].Caption:=inttostr(mass[0]);
    exit;
  end;
  massS[k-1].Caption:=inttostr(mass[0]);
  mass[0]:=mass[n-1]+mass[0];
  mass[n-1]:=mass[0]-mass[n-1];
  mass[0]:=mass[0]-mass[n-1];
  draw;
  n:=n-1;
  heapify (0, 2);
  k:=k+1;
end;

procedure TForm1.Draw;
begin
  for i:= 0 to n-1 do
    massP[i].Caption:=IntToStr(mass[i]);
end;


end.
